package com.xunua.mymycsjsdktest;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;

import com.blankj.utilcode.util.ScreenUtils;
import com.bytedance.sdk.openadsdk.AdSlot;
import com.bytedance.sdk.openadsdk.TTAdManager;
import com.bytedance.sdk.openadsdk.TTAdNative;
import com.bytedance.sdk.openadsdk.TTAdSdk;
import com.bytedance.sdk.openadsdk.TTSplashAd;

public class SplashActivity extends AppCompatActivity {
    private LinearLayout splash_line;
    private TTAdNative adNative;
    private boolean iscomplete=false;

    //开屏广告加载超时时间,建议大于3000,这里为了冷启动第一次加载到广告并且展示,示例设置了3000ms
    private static final int AD_TIME_OUT = 3000;
    private String mCodeId = "801121648";
    private boolean mIsExpress = false; //是否请求模板广告


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash);
        init();
    }

    private void init() {
        splash_line=findViewById(R.id.splash_line);
        TTAdManager ttAdManager = TTAdSdk.getAdManager();//TTAdManager对象为整个SDK的入口接口，可用于广告获取、权限请求、版本号获取等。
//        ttAdManager.requestPermissionIfNecessary(this);//部分机型需要主动申请权限，如 READ_PHONE_STATE权限
        adNative = ttAdManager.createAdNative(this);//TTAdNative为加载广告的接口，用于加载信息流广告、Banner广告、开屏广告、插屏广告等，并提供了加载回调监听器，建议是Activity的成员变量。
        //step3:创建开屏广告请求参数AdSlot,具体参数含义参考文档
        AdSlot adSlot = null;
        if (mIsExpress) {
            //个性化模板广告需要传入期望广告view的宽、高，单位dp，请传入实际需要的大小，
            //比如：广告下方拼接logo、适配刘海屏等，需要考虑实际广告大小
            float expressViewWidth = ScreenUtils.getScreenWidth();
            float expressViewHeight = 300;
            adSlot = new AdSlot.Builder()
                    .setCodeId(mCodeId)
                    .setSupportDeepLink(true)
                    .setImageAcceptedSize(1080, 1920)
                    //模板广告需要设置期望个性化模板广告的大小,单位dp,代码位是否属于个性化模板广告，请在穿山甲平台查看
                    .setExpressViewAcceptedSize(expressViewWidth, expressViewHeight)
                    .build();
        } else {
            adSlot = new AdSlot.Builder()
                    .setCodeId(mCodeId)
                    .setSupportDeepLink(true)
                    .setImageAcceptedSize(1080, 1920)
                    .build();
        }

        /**
         * 加载广告
         * 广告类型，回调，广告加载超时时间  timeOut为加载开屏广告允许的最长时间
         */
        adNative.loadSplashAd(adSlot, new TTAdNative.SplashAdListener() {
            @Override
            public void onError(int i, String s) {

            }

            @Override
            public void onTimeout() {
                //加载超时
            }

            @Override
            public void onSplashAdLoad(TTSplashAd ttSplashAd) {
                if (ttSplashAd==null)
                    return;
                View splashView = ttSplashAd.getSplashView();
                splash_line.addView(splashView);
                ttSplashAd.setSplashInteractionListener(new TTSplashAd.AdInteractionListener() {
                    @Override
                    public void onAdClicked(View view, int i) {
                        //开屏广告点击
                    }

                    @Override
                    public void onAdShow(View view, int i) {
                        //开屏广告展示
                    }

                    @Override
                    public void onAdSkip() {
                        //开屏广告跳过
                        if (iscomplete)
                            return;
                        finish();
                        startActivity(new Intent(SplashActivity.this,MainActivity.class));
                        iscomplete=true;
                    }

                    @Override
                    public void onAdTimeOver() {
                        //开屏广告倒计时结束
                        if (iscomplete)
                            return;
                        finish();
                        startActivity(new Intent(SplashActivity.this,MainActivity.class));
                        iscomplete=true;
                    }
                });
            }
        },AD_TIME_OUT);
    }
}